using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroEquipWearPetInfo : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_HeroEquipWearPetInfo
        {
            #region ObjectBinding Generate 
            public UnityEngine.GameObject skillCell { protected set; get; }
            public IQIGame.Onigao.Framework.ExText skillDes { protected set; get; }
            public IQIGame.Onigao.Framework.ExText useCD { protected set; get; }
            public IQIGame.Onigao.Framework.ExText defaultCD { protected set; get; }
            public UnityEngine.GameObject petInitAttr { protected set; get; }
            public UnityEngine.GameObject slotCell { protected set; get; }
            public UnityEngine.GameObject pabilityGroup { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("skillCell", out var __tbv0);
                this.skillCell = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("skillDes", out var __tbv1);
                this.skillDes = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("useCD", out var __tbv2);
                this.useCD = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("defaultCD", out var __tbv3);
                this.defaultCD = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("petInitAttr", out var __tbv4);
                this.petInitAttr = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("slotCell", out var __tbv5);
                this.slotCell = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("pabilityGroup", out var __tbv6);
                this.pabilityGroup = __tbv6;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private UIN_HeroSkillCell skill;
        private ListScrollAdapter<UIN_CommonTitleValue> initAttrs;
        private ListScrollAdapter<UIN_PetSlotCell> slots;
        private ListScrollAdapter<UIN_PetPabilityGroup> pabilityGroups;

        private Pet pet = null;
        private int slotCount = 0;
        #endregion fields

        #region properties

        protected UIB_UIN_HeroEquipWearPetInfo ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroEquipWearPetInfo();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.skill = this.InitChildNode<UIN_HeroSkillCell>(this.ui.skillCell);
            this.initAttrs = this.InitListScroll<UIN_CommonTitleValue>(this.ui.petInitAttr, this.OnInitAttrChanged);
            this.slots = this.InitListScroll<UIN_PetSlotCell>(this.ui.slotCell, this.OnSlotChanged);

            this.pabilityGroups = this.InitListScroll<UIN_PetPabilityGroup>(this.ui.pabilityGroup, this.OnPabilityGroupChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            this.ClearData();
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds

        public void Show(Pet pet)
        {
            if (pet == null)
            {
                this.Hide();
                return;
            }

            this.SetData(pet);
            this.Show();

            this.ShowSkill();
            this.ShowInitAttrs();
            this.ShowSlots();
            this.ShowPabilityGroups();
        }

        private void SetData(Pet pet)
        {
            this.pet = pet;
            var cfg = this.pet.cfgData;
            this.slotCount = cfg.RandomPabilitySlotUnlockLv.Count + cfg.ExtraSlotNum;
        }

        private void ClearData()
        {
            this.pet = null;
        }

        private void ShowSkill()
        {
            var skillData = this.pet.skillData;
            var cfgSkill = skillData.cfgData;
            this.skill.Show(skillData);
            this.skill.ShowName(cfgSkill.Name);

            this.ui.skillDes.text = skillData.cfgLv.EffectText;

            this.ui.defaultCD.text = APIUIPet.GetStrInitialCoolDown(cfgSkill.InitialCoolDown);
            this.ui.useCD.text = APIUIPet.GetStrCoolDown(cfgSkill.CoolDown);
        }

        private void ShowInitAttrs()
        {
            var cfg = this.pet.cfgData;
            this.initAttrs.RefillCells(cfg.AttType.Count);
        }

        private void OnInitAttrChanged(UIN_CommonTitleValue item, int index)
        {
            var cfg = this.pet.cfgData;
            int cid = cfg.AttType[index];
            int val = cfg.AttValue[index];
            var cfgAttribute = TableCenter.attribute.Get(cid);
            string strVal = "+" + APIAttributeUIUtils.GetStrAttributeValue(cfgAttribute.ShowType, val);
            item.Show(strVal, cfgAttribute.Name);
        }

        private void ShowSlots()
        {
            int cnt = this.slotCount;
            this.slots.RefillCells(cnt);
        }

        private void OnSlotChanged(UIN_PetSlotCell item, int index)
        {
            var slotType = this.GetSlotType(index);
            item.Show(slotType, index, null);
        }

        private UIPetSlotType GetSlotType(int index)
        {
            int numCurr = index + 1;
            int useCount = this.pet.GetUsedSlotCount();
            if (useCount >= numCurr)
            {
                return UIPetSlotType.Used;
            }

            int itemUnlockCount = this.pet.unlockSlot;
            int lvUnlockCount = this.GetLvUnlockSlotCount();
            int unLockCount = itemUnlockCount + lvUnlockCount;
            if (unLockCount >= numCurr)
            {
                return UIPetSlotType.Unlock;
            }
            int lvCount = this.pet.cfgData.RandomPabilitySlotUnlockLv.Count;
            int numLv = numCurr - itemUnlockCount;
            if (lvCount >= numLv)
            {
                return UIPetSlotType.LockByLv;
            }
            return UIPetSlotType.LockByItem;
        }

        private int GetLvUnlockSlotCount()
        {
            int cnt = 0;
            foreach (var unlockLv in this.pet.cfgData.RandomPabilitySlotUnlockLv)
            {
                if (this.pet.level >= unlockLv)
                    cnt++;
            }
            return cnt;
        }

        private void ShowPabilityGroups()
        {
            int cnt = 3;
            this.pabilityGroups.RefillCells(cnt);
        }

        private void OnPabilityGroupChanged(UIN_PetPabilityGroup item, int index)
        {
            item.Show(this.pet, index, null);
        }
        #endregion methonds
    }
}
